package com.example.jiaju.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.motion.widget.MotionLayout;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SearchView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.jiaju.R;
import com.example.jiaju.ui.home.BuildHomeFragment;
import com.example.jiaju.ui.home.FurnitureHomeFragment;
import com.example.jiaju.ui.main.DecorationActivity;
import com.example.jiaju.ui.main.FurnitureActivity;
import com.example.jiaju.ui.main.HouseActivity;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.indicator.RoundLinesIndicator;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页
 */
public class HomeFragment extends Fragment {

    private SearchView searchView;
    private MotionLayout motionLayout;
    private AppBarLayout appBarLayout;
    private Banner banner;
    private ImageView scan;

    private ConstraintLayout arFurnitureBtn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_home,container,false);

//自定义ActionBar
//        Toolbar toolbar=(Toolbar) view.findViewById(R.id.toolbar);
//        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
//        setHasOptionsMenu(true);

// 加载motionlayout
        motionLayout=view.findViewById(R.id.head_bar);
        appBarLayout=view.findViewById(R.id.appbar);
        appBarLayout.addOnOffsetChangedListener((appBarLayout, verticalOffset) ->
                motionLayout.setProgress(-verticalOffset/(float)appBarLayout.getTotalScrollRange()));
//轮播图
        banner=view.findViewById(R.id.banner);
        List<String> bannerList=new ArrayList<>();
        bannerList.add("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202211162304481.jpg");
        bannerList.add("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202211162305622.jpg");
        bannerList.add("https://starry-lixu.oss-cn-hangzhou.aliyuncs.com/202211162305999.jpg");


        banner.setAdapter(new BannerImageAdapter<String>(bannerList) {
            @Override
            public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                Glide.with(holder.imageView)
                        .load(data)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners( 20)))//加载圆角图片
                        .into(holder.imageView);
            }
        }).addBannerLifecycleObserver(this);
        banner.setIndicator(new RoundLinesIndicator(getActivity()));//改变轮播图的指向器的类型
        //魅族样式
        banner.setBannerGalleryMZ(30,0.8f);

//ViewPage2+TabLayout
        TabLayout home_tab=view.findViewById(R.id.home_tab);
        ViewPager2 home_viewpage=view.findViewById(R.id.home_viewpage);
        home_viewpage.setAdapter(new FragmentStateAdapter(this) {
            @Override
            public int getItemCount() {
                return 3;
            }

            @NonNull
            @Override
            public Fragment createFragment(int position) {
                switch (position){
                    case 0:return new HomeViewFragment(0,getContext());
                    case 1:return new HomeViewFragment(1,getContext());
                    default:return new HomeViewFragment(2,getContext());
                }
            }

        });
        new TabLayoutMediator(home_tab,home_viewpage,(tab, position) -> {
            switch (position){
                case 0:tab.setText("楼盘");break;
                case 1:tab.setText("装修");break;
                default:tab.setText("家具");break;
            }

        }).attach();

        //AR家具点击事件
        view.findViewById(R.id.AR_furniture).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), FurnitureActivity.class);
                startActivity(intent);
            }
        });
        //AR新房点击事件
        view.findViewById(R.id.AR_newHourse).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), HouseActivity.class);
                startActivity(intent);
            }
        });
        //AR装修点击事件
        view.findViewById(R.id.AR_decoratione).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), DecorationActivity.class);
                startActivity(intent);
            }
        });


//加载Home_ItemFragment
//        getChildFragmentManager().beginTransaction().replace(R.id.Home_body,home_itemFragment).commit();
        return view;
    }

// 将菜单添加到Bar上面
    //    @Override
    //    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
    //        inflater.inflate(R.menu.search_item,menu);
    //        super.onCreateOptionsMenu(menu, inflater);
    //    }

}
